package com.lib.torment.androidlib.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lib.torment.androidlib.widget.DividerItemDecoration;

import static com.lib.torment.androidlib.core.AppConfigs.REFRESH_STATE_INIT;
import static com.lib.torment.androidlib.core.AppConfigs.REFRESH_STATE_REFRESH;
import static com.lib.torment.androidlib.ui.AppActivity.SWIPE_REFRESH_COLOR;

/**
 * Created by Torment on 2016/9/28.
 */
public abstract class AppFragment extends Fragment {
    protected RecyclerView mRecyclerView;
    protected LinearLayoutManager mLinearLayoutManager;

    protected SwipeRefreshLayout mSwipeRefreshLayout;

    protected View mRootView;

    /**
     * 记录当前Activity的加载状态，默认为初始化。<br/>
     * 通过这个标识可以判断是否加载网络失败的视图
     */
    protected int mRefreshState = REFRESH_STATE_INIT;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(getFragmentLayoutId(), container, false);
        }
        return mRootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupViews();
    }

    /**
     * 主布局ID
     *
     * @return
     */
    protected abstract int getFragmentLayoutId();

    /**
     * 初始化空间
     */
    protected abstract void setupViews();

    protected View findViewById(int id) {
        return mRootView.findViewById(id);
    }

    protected RecyclerView getLinearLayoutRecyclerView(int recyclerViewResId, int orientation) {
        return getLinearLayoutRecyclerView(recyclerViewResId, orientation, false);
    }
    /**
     * 获得一个线性的 RecyclerView
     *
     * @param orientation      水平还是垂直
     * @param isItemDecoration 是否添加分割线
     * @return RecyclerView
     */
    protected RecyclerView getLinearLayoutRecyclerView(int recyclerViewResId, int orientation, boolean isItemDecoration) {
        if (mRecyclerView == null) {
            mRecyclerView = (RecyclerView) findViewById(recyclerViewResId);
            if (mRecyclerView != null) {
                mLinearLayoutManager = new LinearLayoutManager(getActivity());
                mLinearLayoutManager.setOrientation(orientation);
                mRecyclerView.setLayoutManager(mLinearLayoutManager);

                if (isItemDecoration) {
                    mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
                }
            }
        }
        return mRecyclerView;
    }

    /**
     * 获得 SwipeRefreshLayout，通过传入true来判断初始加载操作
     *
     * @param isRefresh 是否为初始化刷新操作
     * @return SwipeRefreshLayout
     */
    protected SwipeRefreshLayout getSwipeRefreshLayout(int swipeRefreshResid, boolean isRefresh, final boolean isShowRefresh) {
        if (mSwipeRefreshLayout == null) {
            mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(swipeRefreshResid);
            mSwipeRefreshLayout.setColorSchemeResources(SWIPE_REFRESH_COLOR);
            if (isRefresh) {
                mSwipeRefreshLayout.post(new Runnable() {
                    @Override
                    public void run() {
                        if (isShowRefresh) {
                            openSwipeRefreshLayoutRefresh();
                        }
                        AppFragment.this.onRefresh(REFRESH_STATE_INIT);
                        mRefreshState = REFRESH_STATE_INIT;
                    }
                });
            }

            mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

                @Override
                public void onRefresh() {
                    AppFragment.this.onRefresh(REFRESH_STATE_REFRESH);
                    mRefreshState = REFRESH_STATE_REFRESH;
                }
            });
        }
        return mSwipeRefreshLayout;
    }

    public SwipeRefreshLayout openSwipeRefreshLayoutRefresh() {
        return setSwipeRefreshLayoutRefreshState(true);
    }

    public SwipeRefreshLayout closeSwipeRefreshLayoutRefresh() {
        return setSwipeRefreshLayoutRefreshState(false);
    }

    public SwipeRefreshLayout setSwipeRefreshLayoutRefreshState(boolean isRefresh) {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setRefreshing(isRefresh);
        }
        return mSwipeRefreshLayout;
    }

    /**
     * 刷新需要重写的方法
     *
     * @param refreshState
     */
    protected void onRefresh(int refreshState) {
    }
}
